package com.zidian.study.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.zidian.student.trucks.R;


/**
 * @{# BottomDialogUtil.java Create on 2016-5-treasure_chest16 上午11:47:39
 * <p>
 * class desc:
 * 1.底部的对话框 动态配置
 * 2.中间的对话框
 * 3.公共的对话框
 * </p>
 */
public class CommonDialogUtil {

    private static AlertDialog bottomDialog;

    /**
     * 底部动态对话框配置
     *
     * @param context
     * @return
     */
    public static AlertDialog getBottomDialog(Context context) {
        //final Activity activity = (Activity) context;
        bottomDialog = new AlertDialog.Builder(context).create();
        bottomDialog.show();
        Window window = bottomDialog.getWindow();
        window.setContentView(R.layout.global_bottom_dialog);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = LayoutParams.MATCH_PARENT;
        layoutParams.height = LayoutParams.WRAP_CONTENT;
        window.setAttributes(layoutParams);
        window.setGravity(Gravity.CENTER);
        bottomDialog.setCancelable(true);
        bottomDialog.setCanceledOnTouchOutside(true);

//        window.setWindowAnimations(R.style.global_dialog_windowanim_style_bottom);

        // 取消
        window.findViewById(R.id.dialogCancle)
                .setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        bottomDialog.dismiss();
                    }
                });
        return bottomDialog;
    }

    private static AlertDialog centerDialog;

    public static AlertDialog getCenterDialog(Context context, String content) {
        centerDialog = new AlertDialog.Builder(context).create();
        centerDialog.show();
        Window window = centerDialog.getWindow();
        window.setContentView(R.layout.global_center_dialog);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = LayoutParams.MATCH_PARENT;
        layoutParams.height = LayoutParams.WRAP_CONTENT;
        window.setAttributes(layoutParams);
        window.setGravity(Gravity.CENTER);
        centerDialog.setCancelable(true);
        centerDialog.setCanceledOnTouchOutside(true);
        //设置中间的内容
        window.setWindowAnimations(R.style.global_dialog_windowanim_style_bottom);
        TextView contentTv = (TextView) window.findViewById(R.id.content);
        contentTv.setText(content);

        // 取消
        window.findViewById(R.id.dia_cancle)
                .setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        centerDialog.dismiss();
                    }
                });
        return centerDialog;
    }

    private static AlertDialog commonDialog;

    /**
     * 公共的Dialog
     *
     * @param context 上下文
     * @param res     视图
     * @param gravity 位置
     * @return
     */
    public static AlertDialog getCommonDialog(Context context, int res, int gravity) {
        commonDialog = new AlertDialog.Builder(context).create();
        commonDialog.show();
        Window window = commonDialog.getWindow();
        window.setContentView(res);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = LayoutParams.MATCH_PARENT;
        layoutParams.height = LayoutParams.WRAP_CONTENT;
        window.setAttributes(layoutParams);
        window.setGravity(gravity);
        commonDialog.setCancelable(true);
        commonDialog.setCanceledOnTouchOutside(true);

        window.setWindowAnimations(R.style.global_dialog_windowanim_style_bottom);
        return commonDialog;
    }

}
